﻿using MettingOrder.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Http;

namespace MettingOrder.Controllers
{
    public class UserController : BaseApiController
    {
        /// <summary>
        /// 从微信中导入数据
        /// </summary>
        /// <returns></returns>
        public Result Import()
        {
            var departmentList = Common.DepartmentApi.Department.getAllDepartment();
            if (departmentList.department != null)
            {
                using (var tran = db.GetTransaction())
                {
                    db.Delete<Department>("where 1=1");
                    foreach (var item in departmentList.department)
                    {
                        Department department = new Department()
                        {
                            Id = item.id,
                            Name = item.name,
                            OrderId = item.order,
                            ParentId = item.parentid,
                        };
                        db.Insert(department);
                    }

                    var userlist = Common.UserApi.User.getAllUser(1);
                    if (userlist != null)
                    {
                        foreach (var user in userlist)
                        {
                            var userInDb = db.FirstOrDefault<Employee>("where userid=@0", user.userid);
                            var employee = new Employee
                            {
                                Name = user.name,
                                Avatar = user.avatar ?? "",
                                DepartMentId = ToString(user.department),
                                Email = user.email ?? "",
                                Mobile = user.mobile ?? "",
                                Position = user.position ?? "",
                                UserStatus = user.status,
                                UserId = user.userid ?? "",
                                WeixinId = user.weixinid ?? ""
                            };
                            if (userInDb == null)
                            {
                                db.Insert(employee);
                            }
                            else
                            {
                                db.Update(employee, userInDb.Id);
                            }
                        }
                    }

                    tran.Complete();
                }
            }
            return JsonHelper.Success(departmentList);
        }
        private string ToString(int[] arr)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(",");
            for (var i = 0; i < arr.Length; i++)
            {
                sb.Append(arr[i]);
                sb.Append(",");
            }
            var str = sb.ToString();
            return str;
        }

        public Result GetUserAll(int id)
        {
            var userlist = Common.UserApi.User.getAllUser(id);
            var g = userlist.GroupBy(s => s.name);
            return JsonHelper.Success(g);
        }
    }
}